package com.zkthink.ceres.file.controller;

import com.zkthink.base.R;
import com.zkthink.base.controller.SuperController;
import com.zkthink.ceres.file.dto.ExcelTemplatePageDTO;
import com.zkthink.ceres.file.dto.ExcelTemplateSaveDTO;
import com.zkthink.ceres.file.dto.ExcelTemplateUpdateDTO;
import com.zkthink.ceres.file.entity.ExcelTemplate;
import com.zkthink.ceres.file.service.ExcelTemplateService;
import com.zkthink.ceres.file.vo.ExcelTemplateVO;
import com.zkthink.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ExcelTemplateController
 * @Description: ExcelTemplateController
 * @Author ck
 * @Date 2021/1/22
 * @Version V1.0
 **/
@Validated
@RestController
@RequestMapping("/excelTemplate")
@Slf4j
@Api(value = "模板上传", tags = "模板上传")
public class ExcelTemplateController extends SuperController<ExcelTemplateService, Long, ExcelTemplate, ExcelTemplatePageDTO, ExcelTemplateSaveDTO, ExcelTemplateUpdateDTO> {
    @Autowired
    private ExcelTemplateService excelTemplateService;

    /**
     * @param
     * @return
     * @author ceres
     * @date 2021-01-22 16:28
     */
    @ApiOperation(value = "查询模板excel", notes = "查询模板excel ")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @SysLog("查询模板excel")
    public R<ExcelTemplateVO> searchByTemplateName(@RequestBody ExcelTemplatePageDTO dto) {
        return excelTemplateService.selectByTemplateName(dto);
    }

    @ApiOperation(value = "添加模板excel", notes = "添加模板excel ")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SysLog("添加模板excel")
    public R addExcelTemplate(@RequestBody ExcelTemplateSaveDTO dto) {
        ExcelTemplate excelTemplate = new ExcelTemplate();
        BeanUtils.copyProperties(dto, excelTemplate);
        excelTemplateService.save(excelTemplate);
        return R.success();
    }

    @ApiOperation(value = "更新模板excel", notes = "更新模板excel ")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @SysLog("更新模板excel")
    public R updateExcelTemplate(@RequestBody ExcelTemplateUpdateDTO dto) {
        ExcelTemplate excelTemplate = new ExcelTemplate();
        BeanUtils.copyProperties(dto, excelTemplate);
        excelTemplateService.updateById(excelTemplate);
        return R.success();
    }

    @ApiOperation(value = "删除模板excel", notes = "删除模板excel ")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SysLog("删除模板excel")
    public R deleteExcelTemplate(@RequestBody ExcelTemplateUpdateDTO dto) {
        excelTemplateService.removeById(dto.getId());
        return R.success();
    }
}
